package _interview150;

/**
 * 190. 颠倒二进制位
 */
public class No190 {
    /**
     * 1. 模拟
     */
    public int reverseBits1(int n) {
        int[] bits = new int[32];
        for (int i = 0; i < 32; i++) {
            bits[i] = (n & (1 << i)) == 0 ? 0 : 1;
        }
        int ans = 0;
        for (int i = 0; i < 32; i++) {
            ans |= bits[31 - i] << i;
        }
        return ans;
    }

    /**
     * 2. 空间优化
     */
    public int reverseBits2(int n) {
        int ans = 0;
        for (int i = 0; i < 32; i++) {
            ans |= ((n & (1 << 31 - i)) == 0 ? 0 : 1) << i;
        }
        return ans;
    }
}
